﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Excel = Microsoft.Office.Interop.Excel;
using Office = Microsoft.Office.Core;
using Microsoft.Office.Tools.Excel;
using Microsoft.Office.Tools.Ribbon;
using System.Windows.Forms;
using RedmineToolBar.Redmine.Downloader;
using RedmineToolBar.Redmine.Configuration;
using RedmineToolBar.Utility;

namespace RedmineToolBar
{
    public partial class RedmineRibbon
    {
        private void Ribbon1_Load(object sender, RibbonUIEventArgs e)
        {

            var app = new Microsoft.Office.Interop.Excel.Application();
            System.Threading.Thread.CurrentThread.CurrentUICulture =
                new System.Globalization.CultureInfo(
                    app.LanguageSettings.get_LanguageID(
                        Microsoft.Office.Core.MsoAppLanguageID.msoLanguageIDUI));

            Redmine.Label = Properties.Resources.RedmineLabel;
            SetupGroup.Label = Properties.Resources.SetupGroupLabel;
            UpdateGroup.Label = Properties.Resources.UpdateGroupLabel;
            btnConfig.Label = Properties.Resources.btnConfigLabel; //"Connessione";
            btnSetupQuery.Label = Properties.Resources.btnSetupQueryLabel;
            btnUpdate.Label = Properties.Resources.btnUpdateLabel;

            

        }

        void SetRibbonInitialVisibility(object sender, EventArgs e)
        {
            RedmineConfigurationBuilder rb = new RedmineConfigurationBuilder();
            btnConfig.Enabled = true;
            if (rb.isWorkbookConnected())
            {
                btnSetupQuery.Enabled = false;
                if (rb.isQuerySelected())
                    btnUpdate.Enabled = false;

            }

        }


        private void btnConfig_Click(object sender, RibbonControlEventArgs e)
        {


            frmConfigRedmine rm = new frmConfigRedmine();
            rm.Show();
            //RedmineConfigurationBuilder rb = new RedmineConfigurationBuilder();
            //if (rb.isWorkbookConnected())
            //{
            //    btnSetupQuery.Enabled = true;
            //    if (rb.isQuerySelected())
            //        btnUpdate.Enabled = false;
            //}

        }

        private void btnSetupQuery_Click(object sender, RibbonControlEventArgs e)
        {
            FrmImpostazioni imp = new FrmImpostazioni();
            imp.Show();

        }

        private void btnUpdate_Click(object sender, RibbonControlEventArgs e)
        {


            RedmineConfigurationBuilder rb = new RedmineConfigurationBuilder();
            var conf = rb.GetRedmineCongfiguration();

            Worksheet worksheetHid = null;

            worksheetHid = Globals.Factory.GetVstoObject(Globals.ThisAddIn.Application.ActiveWorkbook.Worksheets["RedmineConfiguration"]);
            RedmineDownloader rd = rb.GetDownloader();


            Worksheet worksheet = null;

            try
            {
                worksheet = Globals.Factory.GetVstoObject(Globals.ThisAddIn.Application.ActiveWorkbook.Worksheets[conf.QueryName]);
                rd.UpdateSheet(worksheet);
                rb.SetLastUpdate(DateTime.Now).Save();
                RibbonUtility.SetEnable(RibbonStatus.CONNECTED_TO_QUERY);

                var esito = MessageBox.Show("Foglio aggiornato con successo", "Segnalazione");

            }
            catch (Exception ex)
            {

                MessageBox.Show(ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }


        }

        private void btnStatus_Click(object sender, RibbonControlEventArgs e)
        {
            RedmineConfigurationBuilder rb = new RedmineConfigurationBuilder();
            if (rb.isWorkbookConnected())
            {

                var esito = MessageBox.Show("La cartella verrà disconnessa da Redmine", "Segnalazione", MessageBoxButtons.OKCancel, MessageBoxIcon.Information);

                if (DialogResult.OK.Equals(esito))
                    rb.Disconnect();

            }
        }
    }
}
